package com.spic.business.alarm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spic.business.alarm.domain.IocWeizhangAlarm;
import com.spic.business.alarm.mapper.FangPengZhuangAlarmDataMapper;
import com.spic.business.alarm.mapper.IocWeizhangAlarmMapper;
import com.spic.business.alarm.service.IFangPengZhuangAlarmDataService;
import com.spic.business.common.core.utils.DateUtil;
import com.spic.business.config.TemplateConfig;
import com.spic.business.hik.service.HkSafeService;
import com.spic.business.violation.supervision.speed.domain.ViolationsSpeedRecord;
import com.spic.business.violation.supervision.speed.mapper.ViolationsSpeedRecordMapper;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 290512
 * @create 2022-04-09 13:48
 */
@Service
public class FangPengZhuangAlarmDataServiceImpl implements IFangPengZhuangAlarmDataService
{
    @Autowired
    private FangPengZhuangAlarmDataMapper fangPengZhuangAlarmDataMapper;

    @Autowired
    private ViolationsSpeedRecordMapper violationsSpeedRecordMapper;

    @Autowired
    private IocWeizhangAlarmMapper iocWeizhangAlarmMapper;

    @Autowired
    private HkSafeService hkSafeService;
    @Autowired
    private TemplateConfig templateConfig;

    @Override
    public List<Map<String, Object>> selectAlarmList(String dateType)
    {
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> m = new HashMap<>();
        Map<String, Object> m2 = new HashMap<>();
        Map<String, Object> m3 = new HashMap<>();
        Map<String, Object> m4 = new HashMap<>();
        Map<String, Object> m5 = new HashMap<>();
        if("1".equals(templateConfig.getYingJian())) {
            m.put("type","SPEED");
            m.put("remark","当前事件风险可控!");
            m2.put("type","NO_SAFETY_HAT");
            m3.put("type","NO_SAFETY_BELT");
            m4.put("type","TICKET_VIOLATION");
            m5.put("type","REGIONAL_INVASION");
            if("day".equals(dateType)) {
                //超速
                m.put("num", 1);
                //未带安全帽
                m2.put("num", 0);
                //不系安全带
                m3.put("num", 0);
                //工作票违章
                m4.put("num", 0);
                //区域入侵
                m5.put("num", 0);
            } else {
                //超速
                m.put("num", 5);
                //未带安全帽
                m2.put("num", 1);
                //不系安全带
                m3.put("num", 0);
                //工作票违章
                m4.put("num", 0);
                //区域入侵
                m5.put("num", 0);
            }
        } else {
            String dateTime="";
            String halTime = "";//未带安全帽列表查询时间
            if("day".equals(dateType))
            {
                LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
                dateTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                halTime = DateUtils.convertDateStr(DateUtils.getZeroDateTime(),
                        DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.YYYY_MM_DDTHH_MM_SSSSSXXX);

//                 dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            }else if("month".equals(dateType)){
                dateTime = DateUtil.format(DateUtil.getMonthStart(new Date()), "yyyy-MM-dd HH:mm:ss");
                halTime = DateUtils.convertDateStr(DateUtils.initDateByMonth(),
                        DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.YYYY_MM_DDTHH_MM_SSSSSXXX);
            }
            int count =0;

            //违章超速
            List<ViolationsSpeedRecord> list1 = violationsSpeedRecordMapper.selectViolationsSpeedRecordByNowDay(DateUtil.format(dateTime));
            //防碰撞系统
            //List<Map<String, Object>> list2 = fangPengZhuangAlarmDataMapper.selectAlarmList(DateUtil.format(dateTime));
            //定点测速
            //List<Map<String, Object>> list3 = fangPengZhuangAlarmDataMapper.selectDdCSAlarm(DateUtil.format(dateTime));
            QueryWrapper<IocWeizhangAlarm> qw = new QueryWrapper<>();
            qw.ge("alarm_time",DateUtil.format(dateTime));
            List<IocWeizhangAlarm> iocWeizhangAlarms = iocWeizhangAlarmMapper.selectList(qw);

            //违章-未带安全帽
            List<Map<String, Object>> list4 = fangPengZhuangAlarmDataMapper.selectHelmetAlarmList(halTime);

            if(list1!=null && list1.size()>0){
                count = list1.size();
            }
//            if(list2!=null && list2.size()>0){
//                count+=list2.size();
//            }
//            if(list3!=null && list3.size()>0){
//                count+=list3.size();
//            }
            //超速
            m.put("num",count);
            m.put("type","SPEED");
            //未带安全帽
            m2.put("num",list4.size());
            m2.put("type","NO_SAFETY_HAT");
            //不系安全带
            List<IocWeizhangAlarm> collect3 = iocWeizhangAlarms.stream().filter(f -> "NO_SAFETY_BELT".equals(f.getType())).collect(Collectors.toList());
            m3.put("num",collect3.size());
            m3.put("type","NO_SAFETY_BELT");
            //工作票违章
            List<IocWeizhangAlarm> collect4 = iocWeizhangAlarms.stream().filter(f -> "TICKET_VIOLATION".equals(f.getType())).collect(Collectors.toList());
            m4.put("num",collect4.size());
            m4.put("type","TICKET_VIOLATION");
            //区域入侵
            List<IocWeizhangAlarm> collect5 = iocWeizhangAlarms.stream().filter(f -> "REGIONAL_INVASION".equals(f.getType())).collect(Collectors.toList());
            m5.put("num",collect5.size());
            m5.put("type","REGIONAL_INVASION");

            if(count>=270){
                m.put("remark","达到预警值，事故预警!");
            }else{
                m.put("remark","当前事件风险可控!");
            }
        }
        returnMap.add(m);
        returnMap.add(m2);
        returnMap.add(m3);
        returnMap.add(m4);
        returnMap.add(m5);
        return returnMap;
    }

    @Override
    public List<Map<String, Object>> selectAlarmDetaliList(String type,String dateType)
    {
        List<Map<String, Object>> returnMap = new ArrayList<>();

        if("SPEED".equals(type)){
            String dateTime="";
            if("day".equals(dateType))
            {
                LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
                dateTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                 dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            }else if("month".equals(dateType)){
                 dateTime = DateUtil.format(DateUtil.getMonthStart(new Date()), "yyyy-MM-dd HH:mm:ss");
            }
                //违章超速
                List<ViolationsSpeedRecord> list1 = violationsSpeedRecordMapper.selectViolationsSpeedRecordByNowDay(DateUtil.format(dateTime));
                //防碰撞系统
                List<Map<String, Object>> list2 = fangPengZhuangAlarmDataMapper.selectAlarmList(DateUtil.format(dateTime));
                //定点测速
                List<Map<String, Object>> list3 = fangPengZhuangAlarmDataMapper.selectDdCSAlarm(DateUtil.format(dateTime));

                if(list1!=null && list1.size()>0){
                    for (ViolationsSpeedRecord map : list1)
                    {
                        Map<String, Object> m = new HashMap<>();
                        m.put("code",map.getVehicleNumber());
                        m.put("time", DateUtil.format(map.getIllegalTime()));
                        m.put("speed",map.getRunningSpeed());
                        returnMap.add(m);
                    }
                }
                if(list2!=null && list2.size()>0){
                    for (Map<String, Object> map : list2)
                    {
                        Map<String, Object> m = new HashMap<>();
                        m.put("code",map.get("source_name")+"");
                        String dispatching_alarm_event_json =  map.get("dispatching_alarm_event_json")+"";
                        JSONObject jsonObject = JSON.parseObject(dispatching_alarm_event_json);
                        String speed = jsonObject.getString("speed");
                        m.put("speed","".equals(speed)?"0.00":String.format("%.2f",Double.parseDouble(speed)));
                        m.put("time",DateUtil.fmtDateToStr(DateUtil.format(map.get("dispatching_alarm_time")+""),"yyyy-MM-dd HH:mm:ss"));
                        returnMap.add(m);
                    }
                }
                if(list3!=null && list3.size()>0){
                    for (Map<String, Object> map : list3)
                    {
                        Map<String, Object> m = new HashMap<>();
                        m.put("code",map.get("plate_number")+"");
                        m.put("speed","".equals(map.get("speed")+"")?"0.00":String.format("%.2f",Double.parseDouble(map.get("speed")+"")));
                        m.put("time",DateUtil.fmtDateToStr(DateUtil.format(map.get("time_recording")+""),"yyyy-MM-dd HH:mm:ss"));
                        returnMap.add(m);
                    }
                }
                return returnMap;
        }else if("NO_SAFETY_HAT".equals(type)){
            String halTime="";
            if("day".equals(dateType))
            {
                halTime = DateUtils.convertDateStr(DateUtils.getZeroDateTime(),
                        DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.YYYY_MM_DDTHH_MM_SSSSSXXX);
            }else if("month".equals(dateType)){
                halTime = DateUtils.convertDateStr(DateUtils.initDateByMonth(),
                        DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.YYYY_MM_DDTHH_MM_SSSSSXXX);
            }
            //违章-未带安全帽
            List<Map<String, Object>> list4 = fangPengZhuangAlarmDataMapper.selectHelmetAlarmList(halTime);
            if(list4!=null && list4.size()>0){
                for (Map<String, Object> map : list4)
                {
                    Map<String, Object> m = new HashMap<>();
                    String cameraIndexCode = map.get("camera_index_code")+"";
                    JSONObject cameraJson = hkSafeService.getCameraInfoByCode(cameraIndexCode);
                    String time = DateUtils.convertDateStr(map.get("happen_time").toString(),
                            DateUtils.YYYY_MM_DDTHH_MM_SSSSSXXX,DateUtils.YYYY_MM_DD_HH_MM_SS);
                    m.put("code",cameraJson.getString("cameraName"));
                    m.put("time", time);
                    m.put("speed","");
                    returnMap.add(m);
                }
            }
            return returnMap;
        }else if("NO_SAFETY_BELT".equals(type)){
            String dateTime="";
            if("day".equals(dateType))
            {
                LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
                dateTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                 dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            }else if("month".equals(dateType)){
                dateTime = DateUtil.format(DateUtil.getMonthStart(new Date()), "yyyy-MM-dd HH:mm:ss");
            }
            QueryWrapper<IocWeizhangAlarm> qw = new QueryWrapper<>();
            qw.ge("alarm_time",DateUtil.format(dateTime));
            List<IocWeizhangAlarm> iocWeizhangAlarms = iocWeizhangAlarmMapper.selectList(qw);

            if(iocWeizhangAlarms!=null && iocWeizhangAlarms.size()>0){
                List<IocWeizhangAlarm> collect2 = iocWeizhangAlarms.stream().filter(f -> "NO_SAFETY_BELT".equals(f.getType())).collect(Collectors.toList());
                for (IocWeizhangAlarm map : collect2)
                {
                    Map<String, Object> m = new HashMap<>();
                    m.put("code",map.getArea());
                    m.put("time", DateUtil.format(map.getAlarmTime()));
                    m.put("speed",map.getCarNumber());
                    returnMap.add(m);
                }
            }
            return returnMap;
        }else if("TICKET_VIOLATION".equals(type)){
            String dateTime="";
            if("day".equals(dateType))
            {
                LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
                dateTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                 dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            }else if("month".equals(dateType)){
                dateTime = DateUtil.format(DateUtil.getMonthStart(new Date()), "yyyy-MM-dd HH:mm:ss");
            }
            QueryWrapper<IocWeizhangAlarm> qw = new QueryWrapper<>();
            qw.ge("alarm_time",DateUtil.format(dateTime));
            List<IocWeizhangAlarm> iocWeizhangAlarms = iocWeizhangAlarmMapper.selectList(qw);

            if(iocWeizhangAlarms!=null && iocWeizhangAlarms.size()>0){
                List<IocWeizhangAlarm> collect2 = iocWeizhangAlarms.stream().filter(f -> "TICKET_VIOLATION".equals(f.getType())).collect(Collectors.toList());
                for (IocWeizhangAlarm map : collect2)
                {
                    Map<String, Object> m = new HashMap<>();
                    m.put("code",map.getArea());
                    m.put("time", DateUtil.format(map.getAlarmTime()));
                    m.put("speed",map.getUser());
                    returnMap.add(m);
                }
            }
            return returnMap;
        }else if("REGIONAL_INVASION".equals(type)){
            String dateTime="";
            if("day".equals(dateType))
            {
                LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
                dateTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//                 dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            }else if("month".equals(dateType)){
                dateTime = DateUtil.format(DateUtil.getMonthStart(new Date()), "yyyy-MM-dd HH:mm:ss");
            }
            QueryWrapper<IocWeizhangAlarm> qw = new QueryWrapper<>();
            qw.ge("alarm_time",DateUtil.format(dateTime));
            List<IocWeizhangAlarm> iocWeizhangAlarms = iocWeizhangAlarmMapper.selectList(qw);

            if(iocWeizhangAlarms!=null && iocWeizhangAlarms.size()>0){
                List<IocWeizhangAlarm> collect2 = iocWeizhangAlarms.stream().filter(f -> "REGIONAL_INVASION".equals(f.getType())).collect(Collectors.toList());
                for (IocWeizhangAlarm map : collect2)
                {
                    Map<String, Object> m = new HashMap<>();
                    m.put("code",map.getArea());
                    m.put("time", DateUtil.format(map.getAlarmTime()));
                    m.put("speed",map.getUser());
                    returnMap.add(m);
                }
            }
            return returnMap;
        }
        return returnMap;
    }

    @Override
    public List<String> selectAlarmType()
    {
        return fangPengZhuangAlarmDataMapper.selectAlarmType();
    }


    public static void main(String[] args)
    {
        double one = 0.3213123123;
        String  str = String.format("%.2f",one);
        double four = Double.parseDouble(str);
        System.out.println(four);

        String s = DateUtil.fmtDateToStr(DateUtil.format("2022-04-22T07:34".replace("T"," "),"yyyy-MM-dd HH:mm"), "yyyy-MM-dd HH:mm");
        System.out.println(s);


    }
}
